//
//  ZRBluetooth.m
//  TestFile
//
//  Created by 黄涛 on 2018/7/2.
//  Copyright © 2018年 アポロジャパン. All rights reserved.
//

#import "ZRBluetooth.h"

@interface ZRBluetooth ()<CBCentralManagerDelegate,CBPeripheralDelegate>

@property (nonatomic , strong) NSMutableArray * deviceSource;
@property (nonatomic , strong) CBCentralManager * manager;
@property (nonatomic , strong) CBPeripheral * currentDevice;
@end

static NSString * const zr_uuid = @"";

@implementation ZRBluetooth


#pragma mark - ----------------------- 初始化 -----------------------

- (instancetype)init{
    if (self = [super init]) {
        
        _manager = [[CBCentralManager alloc] initWithDelegate:self queue:nil];
    }
    return self;
}


+ (instancetype)shareManager{
    
    static ZRBluetooth * bluetotth = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        bluetotth = [[self alloc] init];
    });
    
    return bluetotth;
}


#pragma mark - ----------------------- 其他方法 -----------------------
#pragma mark - 扫描设备
- (void)scanDevice{

    [self.deviceSource removeAllObjects];
    [_manager scanForPeripheralsWithServices:nil options:nil];
}

#pragma mark - 停止扫描
- (void)stopScan{
    
    [_manager stopScan];
    
}

#pragma mark - 断开连接
- (void)disconnect{
    
    [_manager cancelPeripheralConnection:self.currentDevice];
}

#pragma mark - 连接设备
- (void)connected:(CBPeripheral *)peripheral{
    
    self.currentDevice = peripheral;
    [_manager connectPeripheral:peripheral options:nil];
}

#pragma mark - 获取 已连接设备
- (NSArray *)getConnectedDevices{
    
    return [_manager retrieveConnectedPeripheralsWithServices:@[[CBUUID UUIDWithString:zr_uuid]]];
}

#pragma mark - ----------------------- 代理方法 -----------------------
/** 手机 蓝牙开启状态 */
- (void)centralManagerDidUpdateState:(CBCentralManager *)central{
    
    if (central.state == CBManagerStatePoweredOn) {
        [self scanDevice];
        
    }else{
        
        
    }
    
}

/** 查找 正在广播的 外设 */
- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary<NSString *,id> *)advertisementData RSSI:(NSNumber *)RSSI{
    
    if (![self.deviceSource containsObject:peripheral]) {
        
         [self.deviceSource addObject:peripheral];
        
        ![self.delegate respondsToSelector:@selector(getScanDevicesWithPeripheral:)] ? :
         [self.delegate getScanDevicesWithPeripheral:peripheral];
        

    }
    
}

/** 连接成功 */
- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{
    
    
}

/** 连接失败 */
- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{
    
    
}



#pragma mark - ----------------------- 活动监听 -----------------------
#pragma mark - ---------------------- set & get ----------------------
- (NSMutableArray *)deviceSource{
    if (!_deviceSource) {
        _deviceSource = [[NSMutableArray alloc] init];
    }
    return _deviceSource;
}

@end
